﻿Imports System.IO
Imports System.Net


Public Class VersionChecker
public Property NServerVersion As String =""
Public Property NLocalVersion As String =""
    ''' <summary>
    ''' 检查版本是否一致，一致是返回true ,否则返回false
    ''' </summary>
    ''' <returns></returns>
    Public Function CheckForUpdates(serverUrl As String, LocalVersionFilePath As String) As Boolean 
        Dim localVersion As String = ReadLocalVersion(LocalVersionFilePath)
        Dim serverVersion As String = GetServerVersion(serverUrl)
        NServerVersion=serverVersion
        NLocalVersion=localVersion
        ' 比较两个版本
        If Not localVersion.Equals(serverVersion, StringComparison.OrdinalIgnoreCase) Then
            Return true 
        Else
            ' 版本相同，不需要更新
            return False 
        End If
    End Function 

    Private Function ReadLocalVersion(LocalVersionFilePath) As String
        ' 尝试读取本地版本文件
        If File.Exists(LocalVersionFilePath) Then
            Return File.ReadAllText(LocalVersionFilePath).Trim()
        Else
            ' 如果文件不存在，可能意味着没有安装或首次运行
            Return String.Empty
        End If
    End Function

    Private Function GetServerVersion(serverUrl As String) As String
        Try
            ' 使用WebClient下载服务器上的版本文件内容
            Using client As New WebClient()
                Return client.DownloadString(serverUrl).Trim()
            End Using
        Catch ex As WebException
            ' 处理网络错误或其他异常情况
            MessageBox.Show("无法连接到服务器检查更新。" & vbCrLf & ex.Message)
            Return String.Empty
        End Try
    End Function

End Class